package com.gdufe.firesafe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdufe.firesafe.dao.HouseDao;
import com.gdufe.firesafe.dao.InspectorDao;
import com.gdufe.firesafe.model.entity.InspectorEntity;
import com.gdufe.firesafe.exception.RRException;
import com.gdufe.firesafe.service.InspectorService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("inspectorService")
public class InspectorServiceImpl extends ServiceImpl<InspectorDao, InspectorEntity> implements InspectorService {

    @Autowired
    private HouseDao houseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InspectorEntity> page = this.page(
                new Query<InspectorEntity>().getPage(params),
                new QueryWrapper<InspectorEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(InspectorEntity inspectorEntity) {
        //检验楼栋是否存在
        if(houseDao.selectById(inspectorEntity.getBuildingId())==null){
            throw new RRException("该楼栋不存在");
        }
        return super.save(inspectorEntity);
    }

}
